<?php
/**
* 转账打款失败  需要下次从新发起打款
*/
class Withdraw_fail_model extends CI_Model
{
    private $_table  = 'withdraw_fail';
    private $_table1 = 'withdraw';

    /**
     * 插入
     */
    public function insert($postData)
    {
        $data = array(
            'withdraw_id'     => $postData['withdraw_id'],
            'transfer_type'   => $postData['transfer_type'],
            'uid'             => $postData['uid'],
            'amount'          => $postData['amount'],
            'status'          => 1, //待处理
            'new_withdraw_id' => 0, //处理完成 填写新的序号
            'admin_uid'       => $postData['admin_uid'],
            'admin_name'      => $postData['admin_name'],
            'start_time'      => $postData['start_time'], //开始时间
            'created_at'      => date('Y-m-d H:i:s'),
            'updated_at'      => date('Y-m-d H:i:s')
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    /**
     * 更新状态
     */
    public function updateStatus($withdrawId, $data)
    {
        if (is_array($withdrawId)){
            $this->db->where_in('withdraw_id', $withdrawId);
        } else {
            $this->db->where('withdraw_id', $withdrawId);
        }
        return $this->db->update($this->_table, $data);
    }

    /**
     * 查找未处理的失败记录
     */
    public function findUntreated()
    {
        $this->db->select('withdraw_fail.*, withdraw.sid, withdraw.scenery_name');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'withdraw.withdraw_id = withdraw_fail.withdraw_id', 'INNER');
        $this->db->where('withdraw_fail.status', 1); //未处理
        $this->db->where('withdraw.is_from', 3); //扫码购
        return $this->db->get();
    }

    /**
     * 批量插入
     */
    public function insertBatch($parameter)
    {
        if (empty($parameter)) {
            return false;
        }

        $data = array();
        foreach ($parameter as $key => $value) {
            $data[] = array(
                'withdraw_id'     => $value['withdraw_id'],
                'transfer_type'   => $value['transfer_type'],
                'uid'             => $value['uid'],
                'amount'          => $value['amount'],
                'status'          => 1, //待处理
                'new_withdraw_id' => 0, //处理完成 填写新的序号
                'admin_uid'       => $value['admin_uid'],
                'admin_name'      => $value['admin_name'],
                'start_time'      => $value['start_time'], //开始时间
                'created_at'      => date('Y-m-d H:i:s'),
                'updated_at'      => date('Y-m-d H:i:s')
            );
        }
        $this->db->insert_batch($this->_table, $data);
        return $this->db->affected_rows(); //受影响行数
    }

    /**
     * 批量更新
     */
    public function updateBatch($data)
    {
        $this->db->update_batch($this->_table, $data, 'id');
        return $this->db->affected_rows(); //受影响行数
    }
}